//
//  ZHHomeLog.m
//  ZHHomeCommon
//
//  Created by issuser on 2023/3/13.
//

#import "ZHHomeLog.h"
#import <UIKit/UIKit.h>
#import <CommonTools/CommonTools-Swift.h>

@implementation ZHHomeLog


static NSDateFormatter* logDateFoamatter;



+ (void)logWithFormat:(NSString *)format, ...{
//    return;
    XWClassRoomLogType logType = XWClassRoomManager.logType;
    if(logType == XWClassRoomLogTypeNone){
        return;
    }
    if (!logDateFoamatter) {
        logDateFoamatter = [[NSDateFormatter alloc]init];
        [logDateFoamatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    }
    va_list args;
    if (format) {
        va_start(args, format);
        NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
        NSString* time = [logDateFoamatter stringFromDate:[NSDate date]];
        
        NSString *logString = [NSString stringWithFormat:@"date: %@ content:%@", time,message];
        
        va_end(args);
        NSLog(@"%@",logString);
        if(logType != XWClassRoomLogTypeFile){
            return;
        }
        if (YES) {
            [self saveFile:[NSString stringWithFormat:@"%@ %@\n",time,message]];
        }
    }
}


+(void)logForDebug:(NSString*)str
{
    [self logWithFormat:@"%@",str];
}

+ (void)saveFile:(NSString *)content{
    if (![[[UIDevice currentDevice] name] hasSuffix:@"Simulator"]) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName =[NSString stringWithFormat:@"%@_%@.log",@"JXWLog",[self logFileTime]];
        NSString *logDir = [documentsDirectory stringByAppendingPathComponent:@"JXWLog"];
        NSString *logFilePath = [logDir stringByAppendingPathComponent:fileName];
        if (![[NSFileManager defaultManager] fileExistsAtPath:logFilePath]) {
            //创建文件fileName文件名称，contents文件的内容，如果开始没有内容可以设置为nil，attributes文件的属性，初始为nil
            BOOL ret =  [[NSFileManager defaultManager] createDirectoryAtPath:logDir withIntermediateDirectories:YES attributes:nil error:nil];
            NSAssert(ret,@"创建目录失败");
            ret = [[NSFileManager defaultManager] createFileAtPath:logFilePath contents:nil attributes:nil];
            NSAssert(ret,@"创建日志文件失败");
        }
        
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:[content dataUsingEncoding:NSUTF8StringEncoding]]; //追加写入数据
        [fileHandle closeFile];
    }
}


+ (NSString *)logFileTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    return [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
}

+(NSString*)LogFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName =[NSString stringWithFormat:@"%@_%@.log",@"JXWLog",[self logFileTime]];
    NSString *logDir = [documentsDirectory stringByAppendingPathComponent:@"JXWLog"];
    NSString *logFilePath = [logDir stringByAppendingPathComponent:fileName];
    
    NSString* path = [NSString stringWithFormat:@"%@/%@.zip",logDir,[self logFileTime]];
    NSString* crashPath = [self crashLogFilePath];
//    [SSZipArchive createZipFileAtPath:path withFilesAtPaths:@[logFilePath , crashPath]];
    return path;
}

+(NSString*)crashLogFilePath
{
    NSData *logFileData;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName =[NSString stringWithFormat:@"crashLog.txt"];
    NSString *logDir = documentsDirectory;
    NSString *logFilePath = [logDir stringByAppendingPathComponent:fileName];
    if ([[NSFileManager defaultManager] fileExistsAtPath:logFilePath]) {
        logFileData = [[NSData alloc] initWithContentsOfFile:logFilePath];
    }
    
    return logFilePath;
}

+(void)deleteLogWithUrl:(NSURL*)url
{
    [[NSFileManager defaultManager]removeItemAtURL:url error:nil];
}

@end
